﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ASG.CodeGenerator.SQL.TSQL
{
    public class SQLServerCredentials : IDBMServerCredentials<SqlConnection>
    {
        private SqlConnection _Connection;

        private bool _IsIntegratedSecurity;

        public bool IsIntegratedSecurity
        {
            get { return _IsIntegratedSecurity; }
            set { _IsIntegratedSecurity = value; }
        }

        private bool _PersistSecurityInfo;

        public bool PersistSecurityInfo
        {
            get { return _PersistSecurityInfo; }
            set { _PersistSecurityInfo = value; }
        }

        private string _UserID;

        public string UserID
        {
            get { return _UserID; }
            set { _UserID = value; }
        }

        private string _Password;

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public SQLServerCredentials(bool isIntegratedSecurity, bool persistSecurityInfo, string userID, string password)
        {
            _IsIntegratedSecurity = isIntegratedSecurity;
            _PersistSecurityInfo = persistSecurityInfo;
            _UserID = userID;
            _Password = password;
        }

        #region IDBMServerCredentials<SqlConnection> Members

        public SqlConnection GetDBConnection(string serverName)
        {
            if (_IsIntegratedSecurity)
            {
                _Connection = new SqlConnection("server=" + serverName + ";integrated security=SSPI;persist security info=False;");
            }
            else
            {
                _Connection = new SqlConnection("server=" + serverName + ";uid=" + _UserID + ";pwd=" + _Password);
            }
            return _Connection;
        }

        #endregion
    }
}
